package com.newer.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newer.domain.*;
import com.newer.dto.LoginDto;
import com.newer.dto.MaterialSupplierDto;
import com.newer.dto.tableDataDto;
import com.newer.service.MaterialService;
import com.newer.service.PlanDetailsService;
import com.newer.service.PlanService;
import com.newer.service.SupplierService;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author liruping
 * @version 1.0
 * Create by 2022/6/30 15:52
 */

@RestController
@RequestMapping("plan")
public class PlanController {

    @Autowired
    public PlanService planService;

    @Autowired
    public PlanDetailsService planDetailsService;

    @Autowired
    public MaterialService materialService;

    @Autowired
    public SupplierService supplierService;

    @RequestMapping("selectPlan")
    public Page<Plan> selectPlan(int page,int size,String planId){
        Page<Plan> page1=new Page();
        page1.setSize(size);
        page1.setCurrent(page);

        Page<Plan> plans= this.planService.selectPlan(page1,planId);

        return plans;
    }

    @RequestMapping("selectPlanDetails")
    public List selectPlanDetails(String planid){


        List<PlanDetails> planDetails=this.planDetailsService.selectPlanDetails(planid);

        return planDetails;
    }

    @RequestMapping("insertPlanDetails")
    public Boolean insertPlanDetails(@RequestBody List<tableDataDto> tableData, HttpSession session){

        int userid=((LoginDto)session.getAttribute("login")).getUserId();

        Double Sumprice=0.0;
        int Sumcount=0;
        for (int i=0;i<tableData.toArray().length;i++){
            Sumprice += tableData.get(i).getPrice()*tableData.get(i).getCount();
            Sumcount += tableData.get(i).getCount();
        }

        Plan plan=new Plan();

        Date myDate=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String orderid=sdf.format(myDate);
        plan.setUserid(userid);
        plan.setPlanid(orderid);
        plan.setSumcount(Sumcount);
        plan.setSumprice(Sumprice);
        plan.setSupplierid(tableData.get(0).getSupplierid());
        plan.setPlandate(new Date());
        plan.setPlanstate("申请中");

        Boolean plan1=this.planService.save(plan);

        if(plan1){
            for (int i=0;i<tableData.toArray().length;i++) {
                PlanDetails planDetails=new PlanDetails();
                planDetails.setPlandetailsid(null);
                planDetails.setPlanid(plan.getPlanid());
                planDetails.setCount(tableData.get(i).getCount());
                planDetails.setPrice((double) tableData.get(i).getPrice());
                planDetails.setMaterialid(tableData.get(i).getMaterialid());

                this.planDetailsService.save(planDetails);
            }
            return true;
        }
        return false;
    }

    //主管审核状态
    @RequestMapping("updateState")
    public Boolean updateState(String planid,HttpSession session){

        String realname=((LoginDto)session.getAttribute("login")).getRealName();

        Plan plan=new Plan();
        plan.setPlanstate("申请成功");
        plan.setApprover(realname);
        plan.setPlanid(planid);
        this.planService.updateById(plan);
        return true;
    }

    //单表的查询
    @RequestMapping("Materials")
    public MaterialSupplierDto Materials(){
        List<Supplier> suppliers=this.supplierService.list();
        List<Material> materials=this.materialService.list();

        return new MaterialSupplierDto(materials,suppliers);
    }




}
